package com.gpframework.module.controller.module;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gpframework.common.annotation.Log;
import com.gpframework.common.core.controller.BaseController;
import com.gpframework.common.core.domain.AjaxResult;
import com.gpframework.common.core.page.TableDataInfo;
import com.gpframework.common.enums.BusinessType;
import com.gpframework.common.enums.ResourcesBusiness;
import com.gpframework.module.common.domain.CommonResource;
import com.gpframework.module.common.domain.req.CommonResourceReq;
import com.gpframework.module.common.service.ICommonResourceService;
import com.gpframework.module.module.domain.BrandInfo;
import com.gpframework.module.module.domain.req.BrandInfoReq;
import com.gpframework.module.module.domain.req.BrandInfoUpdate;
import com.gpframework.module.module.domain.vo.BrandInfoVo;
import com.gpframework.module.module.service.IBrandInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName BrandInfoController
 * @Author gpframework
 * @Date 2022-03-23
 * @Version 1.0.0
 * @Description 品牌信息Controller
 */
@Validated
@Api(tags = "品牌信息")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@SuppressWarnings({"rawtypes", "unchecked"})
@RequestMapping("/module/brandInfo")
public class BrandInfoController extends BaseController {

    private final IBrandInfoService iBrandInfoService;


    private final ICommonResourceService commonResourceService;

    /**
     * 个性化设置。查询品牌LOGO、背景图等。
     */
    @ApiOperation("个性化设置")
//    @PreAuthorize("@ss.hasPermi('module:brandInfo:add')")
    @GetMapping("/listBrandImage")
    public AjaxResult listBrandImage(CommonResourceReq resourceReq) throws Exception {
        return AjaxResult.builder(() -> {
            QueryWrapper<CommonResource> lqw = new QueryWrapper<>();
            lqw.eq("brand_id", resourceReq.getBrandId());
            lqw.in("business_type", Arrays.asList(ResourcesBusiness.BRAND_COMPANY_LOGO,
                    ResourcesBusiness.BRAND_LOGIN_BACKGROUND, ResourcesBusiness.BRAND_THEME_BACKGROUND));

            return commonResourceService.listVo(lqw);
        });
    }

    /**
     * @Description 分页
     * @Author gpframework
     * @Date 2022-03-23
     * @Param brandInfo
     * @Return TableDataInfo
     */
    @ApiOperation("列表")
//    @PreAuthorize("@ss.hasPermi('module:brandInfo:list')")
    @GetMapping("/list")
    public TableDataInfo<BrandInfoVo> list(BrandInfoReq brandInfoReq) {
        startPage();
        QueryWrapper<BrandInfo> lqw = new QueryWrapper<BrandInfo>();
        brandInfoReq.generatorQuery(lqw, true);
        List<BrandInfoVo> list = iBrandInfoService.listVo(lqw);
        return getDataTable(list);
    }

    /**
     * @Description 修改
     * @Author gpframework
     * @Date 2022-03-23
     * @Param brandInfo
     * @Return TableDataInfo
     */
    @ApiOperation("修改")
//    @PreAuthorize("@ss.hasPermi('module:brandInfo:edit')")
    @Log(title = "修改品牌信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody BrandInfoUpdate brandInfoUpdate) {
        return toAjax(iBrandInfoService.edit(brandInfoUpdate) ? 1 : 0);
    }


    /**
     * @Description 详情
     * @Author gpframework
     * @Date 2022-03-23
     * @Param brandInfo
     * @Return TableDataInfo
     */
    @ApiOperation("详情")
//    @PreAuthorize("@ss.hasPermi('module:brandInfo:query')")
    @GetMapping(value = "/{brandId}")
    public AjaxResult<BrandInfoVo> getInfo(@PathVariable("brandId") Long brandId) {
        return AjaxResult.success(iBrandInfoService.getBrandInfo(brandId));
    }


    /**
     * @Description 修改品牌logo和印章
     * @Author gpframework
     * @Date 2022-03-23
     * @Param brandInfo
     * @Return TableDataInfo
     */
    @ApiOperation("修改品牌logo和印章")
//    @PreAuthorize("@ss.hasPermi('module:brandInfo:edit')")
    @Log(title = "修改品牌logo和印章", businessType = BusinessType.UPDATE)
    @PutMapping("/updateLogoAndSeal")
    public AjaxResult updateLogoAndSeal(@Valid @RequestBody BrandInfoUpdate brandInfoUpdate) {
        BrandInfo brandInfo = Convert.convert(new TypeReference<BrandInfo>() {
        }, brandInfoUpdate);
        return toAjax(iBrandInfoService.updateLogoAndSeal(brandInfo) ? 1 : 0);
    }
}
